package cn.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
@Component
public class HttpUtils {
	//连接池管理器
	private PoolingHttpClientConnectionManager cm;
	
	public HttpUtils() {
		this.cm = new PoolingHttpClientConnectionManager();
		cm.setMaxTotal(100);//设置最大连接数
		cm.setDefaultMaxPerRoute(10);//设置主机最大连接数
	}
	//根据请求地址，下载页面数据
	public String doGetHtml(String url) {
		//获取httpClient对象
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
		//创建httpGet对象
		HttpGet httpGet = new HttpGet(url);
		//设置请求信息
		httpGet.setConfig(getConfig());
		CloseableHttpResponse response = null;
		//使用HttpClient发起请求
		try {
			response = httpClient.execute(httpGet);
			//使用解析响应，返回结果
			if(response.getStatusLine().getStatusCode()==200) {
				//判断响应体不为空
				if(response.getEntity()!=null) {
					String result = EntityUtils.toString(response.getEntity(),"utf8");
					return result;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(response!=null) {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	//下载图片,返回图片名称
	public String doGetImage(String url) {
		//获取httpClient对象
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
		//创建httpGet对象
		HttpGet httpGet = new HttpGet(url);
		//设置请求信息
		httpGet.setConfig(getConfig());
		CloseableHttpResponse response = null;
		//使用HttpClient发起请求
		try {
			response = httpClient.execute(httpGet);
			//使用解析响应，返回结果
			if(response.getStatusLine().getStatusCode()==200) {
				//判断响应体不为空
				if(response.getEntity()!=null) {
					//下载图片
					//获取图片的后缀
					String suffixName = url.substring(url.lastIndexOf("."));
					//重命名图片
					String newName = UUID.randomUUID().toString()+suffixName;
					//下载图片
					//声明outputStream
					OutputStream outputStream =
							new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\images"+newName));
					response.getEntity().writeTo(outputStream);
					//返回图片名称
					return newName;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(response!=null) {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	//设置请求信息
	private RequestConfig getConfig() {
		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(20000)//创建连接的最长时间
				.setConnectionRequestTimeout(20000)//获取连接的最长时间
				.setSocketTimeout(20000)//数据传输的最长时间
				.build();
		return config;
	}
	
}
